<template>
  <div class="container">
    <el-card>
      <!-- 学科名称 -->
      <el-row>
        <el-col :span="18">
          <div class="before">
            <!-- 左侧 -->
            <el-form>
              <el-form-item>
                <span style="font-size: 14px">学科名称 </span>
                <el-input style="width: 180px; margin-left: 5px"></el-input>
                <el-button size="small" style="margin-left: 10px"
                  >清除</el-button
                >
                <el-button size="small" type="primary">搜索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <!-- 右侧 -->
          <template>
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              @click="editSub()"
            >
              新增学科
            </el-button>
          </template>
        </el-col>
      </el-row>
      <!-- 警告 数据记录-->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        style="margin-bottom: 15px"
      >
      </el-alert>
      <!-- 数据 表格 -->
      <el-table :data="dataList" style="width: 100%" size="small ">
        <el-table-column type="index" width="50" label="序号"></el-table-column>
        <el-table-column
          prop="subjectName"
          label="学科名称"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="创建者"
          width="100"
        ></el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.addDate | parseTimeByString }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示" width="110">
          <template slot-scope="scope">
            <span>{{ scope.row.isFrontDisplay === 1 ? "是" : "否" }} </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="twoLevelDirectory"
          label="二级目录"
        ></el-table-column>
        <el-table-column prop="tags" label="标签"></el-table-column>
        <el-table-column prop="totals" label="题目数量"></el-table-column>
        <el-table-column label="操作" fixed="right" width="220">
          <!-- 操作部分 -->
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="
                $router.push(
                  `directorys?id=${scope.row.id}&name=${encodeURIComponent(
                    scope.row.subjectName
                  )}`
                )
              "
              >学科分类</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="
                $router.push(
                  `tags?id=${scope.row.id}&name=${encodeURIComponent(
                    scope.row.subjectName
                  )}`
                )
              "
              >学科标签</el-button
            >
            <el-button type="text" size="small" @click="editSub(scope.row)"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="removeSub(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pages">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="+page.page"
          :page-sizes="[5, 10, 15]"
          :page-size="+page.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="+total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 放置新增学科组件 -->
    <subjects-add
      ref="subjectAdd"
      :show-dialog.sync="showDialog"
      v-on:newDataes="newDataes"
    ></subjects-add>
  </div>
</template>

<script>
import { list, remove, detail } from "@/api/hmmm/subjects";
import subjectsAdd from "../components/subjects-add.vue";

export default {
  components: { subjectsAdd },
  data() {
    return {
      dataList: [],
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
      },
      total: 0, // 记录总数
      showDialog: false, // 显示窗体
    };
  },
  computed: {},
  created() {
    this.getlist();
  },
  methods: {
    async getlist() {
      const result = await list(this.page);
      console.log(result);
      this.dataList = result.data.items;
      console.log(this.dataList);
      // 当前页数
      this.page.page = result.data.page;
      console.log(this.page.page);
      // 所有数据
      this.total = result.data.counts;
      console.log(this.total, "total");
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage; // 将当前页码赋值给当前的最新页码
      this.getlist();
    },

    // 删除
    removeSub(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 如果点击了确定就会进入then
          return remove({ id }); // 返回promise对象
        })
        .then(() => {
          //  如果删除成功了  就会进入这里
          this.$emit("removeSub"); // 触发自定义事件
          this.$message.success("删除成功");
          this.getlist();
        });
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.page.pagesize = val;
      this.getlist();
    },
    // 进入某一页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.page = val;
      this.getlist();
    },
    // 新增用户刷新列表
    newDataes() {
      this.getlist();
    },
    // 新增修改
    editSub(data) {
      // 控制弹窗显示
      this.showDialog = true;
      if (data) {
        console.log(data);
        this.$nextTick(() => {
          this.$refs.subjectAdd.xiugai(data);
        });
      }
    },
  },
};
</script>

<style rel="stylesheet/scss" scoped lang='scss'>
.container {
  margin: 20px;

  .pages {
    margin-top: 10px;
    text-align: right;
  }
}
</style>
